﻿#region License

// Copyright (c) 2011, Matt Holmes
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are permitted provided 
// that the following conditions are met:
// 
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the 
//   following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
//   the following disclaimer in the documentation and/or other materials provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
// OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

using System;
using System.Collections;
using System.Collections.Generic;

namespace SkyrimTweaker.Common {
	public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue> {
		#region Fields

		private readonly IDictionary<TKey, TValue> _dictionary;

		#endregion

		#region Constructors

		public ReadOnlyDictionary () {
			_dictionary = new Dictionary<TKey, TValue> ();
		}

		public ReadOnlyDictionary (IDictionary<TKey, TValue> dictionary) {
			_dictionary = dictionary;
		}

		#endregion

		#region Private Methods

		IEnumerator IEnumerable.GetEnumerator () {
			return (_dictionary as IEnumerable).GetEnumerator ();
		}

		#endregion

		#region IDictionary<TKey,TValue> Members

		public void Add (TKey key, TValue value) {
			throw new NotSupportedException ("This dictionary is read-only");
		}

		public bool ContainsKey (TKey key) {
			return _dictionary.ContainsKey (key);
		}

		public ICollection<TKey> Keys {
			get { return _dictionary.Keys; }
		}

		public bool Remove (TKey key) {
			throw new NotSupportedException ("This dictionary is read-only");
		}

		public bool TryGetValue (TKey key, out TValue value) {
			return _dictionary.TryGetValue (key, out value);
		}

		public ICollection<TValue> Values {
			get { return _dictionary.Values; }
		}

		public TValue this [TKey key] {
			get { return _dictionary[key]; }
			set { throw new NotSupportedException ("This dictionary is read-only"); }
		}

		public void Add (KeyValuePair<TKey, TValue> item) {
			throw new NotSupportedException ("This dictionary is read-only");
		}

		public void Clear () {
			throw new NotSupportedException ("This dictionary is read-only");
		}

		public bool Contains (KeyValuePair<TKey, TValue> item) {
			return _dictionary.Contains (item);
		}

		public void CopyTo (KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
			_dictionary.CopyTo (array, arrayIndex);
		}

		public int Count {
			get { return _dictionary.Count; }
		}

		public bool IsReadOnly {
			get { return true; }
		}

		public bool Remove (KeyValuePair<TKey, TValue> item) {
			throw new NotSupportedException ("This dictionary is read-only");
		}

		public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator () {
			return _dictionary.GetEnumerator ();
		}

		#endregion
	}
}
